package com.songlanyun.modules.comment.controller.api;

import java.util.Arrays;
import java.util.Map;

import com.songlanyun.common.validator.ValidatorUtils;
import com.songlanyun.modules.account.controller.common.AbstractApiController;
import com.songlanyun.modules.comment.entity.UserComment;
import com.songlanyun.modules.comment.service.UserCommentService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.songlanyun.common.utils.PageUtils;
import com.songlanyun.common.utils.R;


/**
 * 会员的评论
 *
 * @author Mark
 * @email sunlightcs@gmail.com
 * @date 2020-11-30 17:57:16
 */
@RestController
@RequestMapping("api/comment/user")
public class UserCommentApiController extends AbstractApiController {
    @Autowired
    private UserCommentService userCommentService;


    /**
     * 我的评论列表
     */
    @GetMapping("/list")
//    @RequiresPermissions("comment:userComment:list")
    public R list(@RequestParam Map<String, Object> params) {
        params.put("commentUserId", setUserId(params));
        PageUtils page = userCommentService.myPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @GetMapping("/info/{id}")
//    @RequiresPermissions("comment:userComment:info")
    public R info(@PathVariable("id") Long id) {
        UserComment userComment = userCommentService.getById(id);

        return R.ok().put("userComment", userComment);
    }

//    /**
//     * 评论
//     */
//    @PostMapping("/save")
////    @RequiresPermissions("comment:userComment:save")
//    public R save(@RequestBody UserComment userComment) {
//        ValidatorUtils.validateEntity(userComment);
//        userComment.setCommentUserId(getUserId(true));
//        userCommentService.save(userComment);
//
//        return R.ok();
//    }


    /**
     * 删除
     */
    @DeleteMapping("/delete/{id}")
//    @RequiresPermissions("comment:userComment:delete")
    public R delete(@PathVariable("id") Long id) {
        UserComment userComment = userCommentService.getById(id, true);
        if (getUserId(true).equals(userComment.getCommentUserId())) {//会员本人才能删除
            userCommentService.removeById(id);
        }

        return R.ok();
    }

}
